<template>
  <blogHeader></blogHeader>

  <!-- 主内容区域 -->
  <main class="container main_min_height  max-w-[1200px] mx-auto p-4">
    <div class="grid grid-cols-10 gap-8">

      <!-- 左边栏，占用 3 列 -->
      <div class="sm:col-span-7 col-span-10 mb-3">
        <!-- 文章 -->
        <!-- 归档列表 -->
        <div v-for="(archive, index) in archives" :key="index"
          class="p-5 mb-4 border border-gray-200 rounded-lg bg-white dark:bg-gray-800 dark:border-gray-700">
          <time class="text-lg font-semibold text-gray-900 dark:text-white">{{ archive.month }}</time>
          <ol class="mt-3 divide-y divider-gray-200 dark:divide-gray-700">
            <li v-for="(article, index2) in archive.articles" :key="index2">
              <a @click="goArticleDetail(article.id)"
              class="items-center block p-3 sm:flex hover:bg-gray-100 dark:hover:bg-gray-700 ">
                <div v-loading="article.cover === ''" class="">
                  <img class="w-24 h-12 object-cover mb-3 mr-3 rounded-lg sm:mb-0" :src="article.cover" alt=" " />
                </div>
                <div class="text-gray-600 dark:text-gray-400">
                  <h2 class="text-base font-normal text-gray-900">
                    {{ article.title }}
                  </h2>
                  <span class="inline-flex items-center text-xs font-normal text-gray-500 dark:text-gray-400">
                    <svg class="inline w-2.5 h-2.5 mr-2 text-gray-400 dark:text-white" aria-hidden="true"
                      xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 20 20">
                      <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                        d="M5 1v3m5-3v3m5-3v3M1 7h18M5 11h10M2 3h16a1 1 0 0 1 1 1v14a1 1 0 0 1-1 1H2a1 1 0 0 1-1-1V4a1 1 0 0 1 1-1Z" />
                    </svg>
                    {{ article.createDate }}
                  </span>
                </div>
              </a>
            </li>
          </ol>
        </div>


        <!-- 分页 -->
        <nav aria-label="Page navigation example" class="mt-10 flex justify-center" v-if="pages > 1">
          <ul class="flex items-center -space-x-px h-10 text-base">
            <!-- 上一页 -->
            <li>
              <a @click="getArchives(current - 1)"
                class="flex items-center justify-center px-4 h-10 ml-0 leading-tight text-gray-500 bg-white border border-gray-300 rounded-l-lg hover:bg-gray-100 hover:text-gray-700 dark:bg-gray-800 dark:border-gray-700 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white"
                :class="[current > 1 ? '' : 'cursor-not-allowed']">
                <span class="sr-only">上一页</span>
                <svg class="w-3 h-3" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none"
                  viewBox="0 0 6 10">
                  <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                    d="M5 1 1 5l4 4" />
                </svg>
              </a>
            </li>
            <!-- 页码 -->
            <li v-for="(pageNo, index) in pages" :key="index">
              <a @click="getArchives(pageNo)"
                class="flex items-center justify-center px-4 h-10 leading-tight border  dark:bg-gray-800 dark:border-gray-700 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white"
                :class="[pageNo == current ? 'text-blue-600  bg-blue-50 border-blue-300 hover:bg-blue-100 hover:text-blue-700' : 'text-gray-500 border-gray-300 bg-white hover:bg-gray-100 hover:text-gray-700']">
                {{ index + 1 }}
              </a>
            </li>
            <!-- 下一页 -->
            <li>
              <a @click="getArchives(current + 1)"
                class="flex items-center justify-center px-4 h-10 leading-tight text-gray-500 bg-white border border-gray-300 rounded-r-lg hover:bg-gray-100 hover:text-gray-700 dark:bg-gray-800 dark:border-gray-700 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white"
                :class="[current < pages ? '' : 'cursor-not-allowed']">
                <span class="sr-only">下一页</span>
                <svg class="w-3 h-3" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none"
                  viewBox="0 0 6 10">
                  <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                    d="m1 9 4-4-4-4" />
                </svg>
              </a>
            </li>
          </ul>
        </nav>
      </div>


      <!-- 右边侧边栏，占用一列 -->
      <aside class="col-span-3 hidden sm:block">
        <!-- 博主信息 -->
        <AuthorInfoCard></AuthorInfoCard>
        <!-- 标签 -->
        <TagCloudCard></TagCloudCard>

      </aside>
    </div>

  </main>
  <div class="hidden md:block sticky bottom-0 right-0 w-full">
        <div class="absolute right-0 bottom-0">
            <live2d></live2d>
        </div>
    </div>
  <indexFooter></indexFooter>


</template>

<script setup>
import { onMounted, ref } from 'vue'
import blogHeader from '@/composition/frontend/component/Header.vue'
import AuthorInfoCard from '@/composition/frontend/component/AuthorInfoCard.vue';
import TagCloudCard from '@/composition/frontend/component/TagCloudCard.vue';
import indexFooter from '@/composition/frontend/component/indexFooter.vue';
import live2d from '@/components/live2d.vue';
import { getArchiveArticlePageList } from '@/api/frontend/archive';
import router from '@/router';


//初始化归档页文章数据
const archives = ref([])
// 当前页码
const current = ref(1)
// 每页显示的文章数
const size = ref(10)
// 总文章数
const total = ref(0)
// 总共多少页
const pages = ref(0)
const getArchives = (currentNo) => {
  //上下页是否能点击判断,上一页页码小于1或者下一页页码大于pages
  if (currentNo < 1 || (pages.value > 0 && currentNo > pages.value)) {
    return
  }
  getArchiveArticlePageList({ current: currentNo, size: size.value }).then(res => {
    if (!res.success) {
      showMessage('获取文章失败', 'error')
      return
    }
    console.log(res)
    archives.value = res.data
    current.value = res.current
    size.value = res.size
    total.value = res.total
    pages.value = res.pages
  }).then(() => {
    for (let i = 0; i < archives.value.length; i++) {   //根据日期分类
      for (let j = 0; j < archives.value[i].articles.length; j++) {   //遍历文章数组
        if (archives.value[i].articles[j].cover !== '') { //如果文章封面为空,则请求随机图片
          continue
        }
        fetch("https://api.maho.cc/random-img/pc.php").then(res => {
          if (!res.ok) {
            showMessage('获取随机图片失败', 'error')
            return
          }
          archives.value[i].articles[j].cover = res.url
        }).catch(() => {
          showMessage('获取随机图片失败', 'error')
        })
      }
    }
  })
}
onMounted(() => {
  getArchives() //初始化归档页数据
})
//=====================跳转详情页=======================
const goArticleDetail = (id) =>{
    router.push('/article/'+ id)
}

</script>

<style scoped></style>